//index.js
//获取应用实例
const app = getApp();
const api = require('../../utils/api.js');
Page({
  data: {
    curView: "solo",
    bindStatus: false,
    bindSteam: false,
    hideMask: false,
    gamers: [],
    gamersTemp: [{
        "nickname": "呵呵呵呵",
      },
      {
        "nickname": "呵呵呵呵",
      },
      {
        "nickname": "呵呵呵呵呵",
      }
    ],
    loading: true,
    num: 2,
    user: {},
    more: true,
    userStatus: '',
    type: 1,
    noFriend: false,
    scene: 1001,
    wins: '',
    groupId: ''
  },
  // 切换tab
  changeView: function(event) {
    let self = this
    let target = event.target;
    let dataset = event.target.dataset;
    let scene = this.data.scene
    if (dataset.view != self.data.curView) {
      switch (dataset.view) {
        case 'solo':
          self.data.curView = 'solo';
          self.setData({
            type: 1
          })
          if (scene == '1044') {
            self.getRank();
          }
          break;
        case 'duo':
          self.data.curView = 'duo';
          self.setData({
            type: 2
          })
          if (scene == '1044') {
            self.getRank();
          }
          break;
        case 'squad':
          self.data.curView = 'squad';
          self.setData({
            type: 3
          })
          if (scene == '1044') {
            self.getRank();
          }
          break;
        case 'friend':
          if (self.data.noFriend == 0) {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: '你没有朋友在玩或是正在加载你的好友信息！',
              success: function() {
                self.getInfo();
              }
            })
          } else if (self.data.noFriend == 1) {
            self.data.curView = 'friend';
            self.setData({
              type: 4
            })
            self.getRank();
          } else if (self.data.noFriend == 3) {
            wx.showModal({
              title: '温馨提示',
              content: '你还没绑定steam账号，请绑定后再来查看你的好友',
              confirmText: '前去绑定',
              success: function(res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: '../bind/bind'
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
          break;
      }

      this.setData({
        curView: this.data.curView
      })
    }
    this.setData({
      num:2
    })
  },
  gotoBind() {
    wx.redirectTo({
      url: '../bindpubg/bindpubg'
    })
  },
  // 解绑操作
  unbind() {
    let self = this
    wx.showModal({
      title: '提示',
      content: '确定取消绑定pubg账号?',
      success: function(res) {
        if (res.confirm) {
          self.unbindFn();
        }
      }
    })
  },
  // 解绑
  unbindFn() {
    let self = this
    api.http("pubg/unbind", "POST", {
      token: wx.getStorageSync("token")
    }).then((res) => {
      if (res.data.code == 1) {
        self.setData({
          bindStatus: false,
          'user.pubg': [],
          userStatus: ''
        });
        self.getInfo()
      } else {
        wx.showToast({
          title: '解绑失败',
          duration: 1000,
          'user.h1z1': null
        })
      }
    })
  },

  // 详情页
  toInfo(e) {
    let self = this
    let id = e.target.id
    let stid = e.target.dataset.stid
    if (id) {
      wx.redirectTo({
        url: '../info/info?id=' + id + '&name=' + self.data.curView
      })
    } else if (stid) {
      wx.redirectTo({
        url: '../info/info?stid=' + stid + '&name=' + self.data.curView
      })
    }
  },
  toMe() {
    let self = this
    let id = wx.getStorageSync("user_id")
    if (this.data.bindStatus && id) {
      wx.redirectTo({
        url: '../info/info?id=' + id + '&name=' + self.data.curView
      })
    }
  },
  // 得到用户绑定信息
  getInfo(userid) {
    let self = this;
    let bindStatus = false;
    let stats = '暂无';
    let friend = false;
    api.http("pubg/getUserInfo", "POST", {
      userId: userid
    }).then((res) => {
      if (res.data.code == 1) {
        let data = res.data.datas;
        if (data.pubg) {
          friend = data.pubg.friends || 0
          bindStatus = true
        }
        self.setData({
          bindStatus: bindStatus,
          user: data,
          gamers: '',
          noFriend: friend,
          loading: true
        })
        self.getRank();
      }
    }).catch((res) => {
      console.log(res)
      self.setData({
        loading: true
      })
    })
  },

  // 获得群排行
  getRank() {
    let self = this
    this.setData({
      gamers: [],
      userStatus: '',
      wins: '',
      loading: false
    })
    if (this.data.groupId == '') {
      api.http('wechat/getGroupId', 'POST', {
        encryptedData: wx.getStorageSync("encryptedData"),
        iv: wx.getStorageSync("iv"),
        token: wx.getStorageSync("token"),
        type: 'pubg'
      }).then((res) => {
        if (res.data.code == 1) {
          self.setData({
            groupId: res.data.datas
          })
          self.ranking()
        } else {
          self.setData({
            loading: true
          })
        }
      }).catch((res) => {
        console.log(res)
        self.setData({
          loading: true
        })
      })
    } else {
      self.ranking()
    }
  },
  ranking() {
    let self = this
    let more = false
    let type = self.data.type
    let hideMask = false
    api.http("group/pubg/ranking", "POST", {
      groupId: this.data.groupId,
      token: wx.getStorageSync("token"),
      type: type
    }).then((res) => {
      if (res.data.code == 1) {
        let data = res.data.datas
        let group = data.group || ''
        let me = data.me || ''
        let wins = me.Wins || data.me.wins || ''
        if (res.data.pages > 1) {
          more = true
        }
        if (self.data.scene == '1044') {
          hideMask = true
        }
        self.setData({
          gamers: group,
          userStatus: me,
          more: more,
          hideMask: hideMask,
          wins: wins,
          loading: true
        })
      } else {
        self.setData({
          loading: true
        })
      }
    }).catch((res) => {
      self.setData({
        loading: true
      })
    })
  },
  //向下滚动加载
  loadMore: function(e) {
    let self = this;
    let num = this.data.num;
    let type = this.data.type;
    let more = false;
    let hideMask = false
    let scene = this.data.scene
    if (this.data.more) {
      this.setData({
        loading: false
      })
      api.http("group/pubg/ranking", "POST", {
        groupId: this.data.groupId,
        token: wx.getStorageSync("token"),
        type: type,
        num: num
      }).then((res) => {
        if (res.data.code == 1) {
          let data = res.data.datas
          let gamers = self.data.gamers
          let group = data.group
          for (var i = 0; i < group.length; i++) {
            gamers.push(group[i])
          }
          if (res.data.pages > res.data.num) {
            more = true
          }
          if (scene == '1044') {
            hideMask = true
          }
          if (group.length > 0) {
            self.setData({
              hideMask: hideMask,
              gamers: gamers,
              loading: true,
              num: ++num,
              more: more
            })
          } else {
            self.setData({
              loading: true
            })
          }
        } else {
          self.setData({
            loading: true
          })
        }
      }).catch((res) => {
        self.setData({
          loading: true
        })
      })
    }
  },
  // 分享
  onShareAppMessage: function() {
    return {
      title: '绝地求生群排行',
      path: 'pages/index/index'
    }
  },
  onLoad: function(ops) {
    let self = this
    let scene = wx.getStorageSync('scene')
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function() {
      let user_id = wx.getStorageSync("user_id");
      if(user_id){
        self.getInfo(user_id);
      }
    })
    this.setData({
      scene: scene
    })

    wx.showShareMenu({
      //要求小程序返回分享目标信息
      withShareTicket: true
    })
  }
})